package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.AppUtil.SecurityUtils;
import com.example.demo.entity.DTO.SelectCarDTO;
import com.example.demo.entity.Goods;
import com.example.demo.entity.VO.CarVO;
import com.example.demo.entity.VO.SelectCarVO;
import com.example.demo.result.MyPage;
import com.example.demo.result.Result;
import com.example.demo.service.IGoodsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import com.example.demo.service.ICarService;
import com.example.demo.entity.Car;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-06
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/car")
@AllArgsConstructor
@Slf4j
public class CarController {

    private final ICarService carService;
    private final IGoodsService goodsService;



    /**
     * 获取用户购物车信息
     *
     * @param selectCarVO 购物车参数对象列表
     * @return
     */
    @ApiOperationSupport(author = "Mr.力铉器")
    @PostMapping("/selectCar")
    @ApiOperation(value = "获取用户购物车信息2.0")
    public Result<List<Object>> getShopCart(@Valid SelectCarVO selectCarVO) {
        List<SelectCarDTO> shopCart = carService.selectCar(selectCarVO.getUserId());
        return Result.success(shopCart).msg("查询成功");
    }
    /**
     * 新增2.0
     * @param carVO 对象
     * @return Result
     */
    @ApiOperationSupport(author = "Mr.sdy")
    @ApiOperation(value = "添加、修改用户购物车物品",consumes = "通过商品id(prodId)、店铺Id(shopId)(我的一个想法，到时候讨论),添加/修改用户购物车商品，并传入改变的商品个数(count)，"+
            "当count为正值时，增加商品数量，当count为负值时，将减去商品的数量，当最终count值小于0时，会将商品从购物车里面删除")
    @PostMapping("/changeItem")
    public Result<String> addItem(@Valid CarVO carVO) {
        if (carVO.getGoodsNum() == 0){
            Result.error("输入更改数量");
        }
        //得到用户的userid
        String userId = SecurityUtils.getUser().getUserId();
        Goods goodsDTO = goodsService.getGoodsByProdId(carVO.getGoodsId());
        log.info("goodsDTO:{}",goodsDTO);

        // 当商品状态不正常时，不能添加到购物车
        if (goodsDTO.getGoodsFlag() == 0){
            return Result.error("商品已下架");
        }
        return Result.success().msg("成功加入购物车");
    }
}